package com.lemonfish.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.vo.JsonResult;
import com.lemonfish.enumcode.CodeEnum;
import com.lemonfish.pojo.entity.GoodsCate;
import com.lemonfish.pojo.entity.GoodsParam;
import com.lemonfish.pojo.vo.GoodsCateVO;
import com.lemonfish.service.GoodsCateService;
import com.lemonfish.util.MyJsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品分类 相关Controller
 *
 * @author Lemonfish
 * @version 1.0
 * @date 2020-03-07
 * Copyright © Chao Zhang
 */
@Api(tags = {"商品分类"})
@RestController
@RequestMapping("/goodsCate")
@Slf4j
public class GoodsCateController extends BaseCrudMappingRestController<GoodsCate, GoodsCateVO> {

    @Autowired
    private GoodsCateService goodsCateService;

    /**
     * 获取分页数据（带树）
     *
     * @param curPage
     * @param pageSize
     * @return
     */
    @GetMapping("/{curPage}/{pageSize}")
    public MyJsonResult showList(@PathVariable("curPage") long curPage, @PathVariable("pageSize") long pageSize) {
        IPage<GoodsCate> page = goodsCateService.showList(curPage, pageSize);
        return MyJsonResult.success(page);
    }


    /**
     * 获取父级分类的子节点（前两级）
     *
     * @return MyJsonResult
     */
    @GetMapping("/parentCate")
    public MyJsonResult getParentCate() {
        QueryWrapper<GoodsCate> queryWrapper = new QueryWrapper<>();
        // 获取前两级的子节点
        queryWrapper.ne("level", 3);
        List<GoodsCate> tree = BeanUtils.buildTree(goodsCateService.getEntityList(queryWrapper));
        return MyJsonResult.success(tree);
    }

    @GetMapping("/cateList")
    public MyJsonResult getCateList() {
        List<GoodsCate> tree = BeanUtils.buildTree(goodsCateService.getEntityList(null));
        return MyJsonResult.success(tree);
    }

    /**
     * 添加或者更新分类
     *
     * @param goodsCate
     * @return
     */
    @PostMapping("/InsertOrUpdate")
    public MyJsonResult insert(@RequestBody GoodsCate goodsCate) {
        System.out.println(goodsCate.getId());
        boolean flag = goodsCateService.createOrUpdateEntity(goodsCate);
        if (flag) {
            return MyJsonResult.success(null);
        } else {
            return MyJsonResult.fail(CodeEnum.FAIL_OPERATION);
        }
    }

    /**
     * 根绝parentId获取节点
     *
     * @param parentId
     * @return
     */
    @GetMapping("/single/{parentId}")
    public MyJsonResult getById(@PathVariable("parentId") long parentId) {
        return MyJsonResult.success(goodsCateService.getEntity(parentId));
    }

    /**
     * 获取分类下面的动态参数或者静态参数
     *
     * @param cateId
     * @param type
     * @return
     */
    @GetMapping("/params/{cateId}/{type}")
    public MyJsonResult getParams(@PathVariable("cateId") long cateId,
                                  @PathVariable("type") String type) {
        List<GoodsParam> paramsList = goodsCateService.getParamsList(cateId, type);
        return MyJsonResult.success(paramsList);
    }

    /**
     * 加载更多数据
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "获取关联数据映射关系")
    @GetMapping("/attachMore")
    public JsonResult attachMore(HttpServletRequest request, ModelMap modelMap) throws Exception {
        Wrapper wrapper = null;
        return new JsonResult(modelMap);
    }
}
